# -*- coding:utf-8 -*-
from database.db_handler import MysqlHander
from my_http import MyHttp
import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re
import threading
import hashlib
import urllib.parse
import time
import timedelta
import datetime
import requests
import json
'''
北京证券交易所
'''

headers={'content-type':'application/x-www-form-urlencoded'}
class BeijingStock:
    def __init__(self):
        self.db = MysqlHander("config.ini")
        self.stock_name = {}
        self.table = ""

    def add_stock(self, code, name):
        insert = "insert into entity_gupiao (Fcode, Fname, Fexchange_id, Fcreate_time, Fmodify_time) values "
        insert += "('" + code + "','" + name + "',3 , now(), now());"
        
        print(insert)
        try:
            #pass
            self.db.execute_not_safe(insert)
        except Exception as e:
            print(str(e))
            pass


    def query_page_data(self, page):
        try:
            t = time.time()
            time_tag = int(round(t * 1000))
            param = "statetypes%5B%5D=P07&page=" + str(page) + "&companyCode=&isNewThree=1&sortfield=updateDate&sorttype=desc&keyword=&needFields%5B%5D=id&needFields%5B%5D=stockCode&needFields%5B%5D=stockName&needFields%5B%5D=companyName&needFields%5B%5D=status&needFields%5B%5D=registerAddress&needFields%5B%5D=sponsorOrg&needFields%5B%5D=appraisalOrg&needFields%5B%5D=lawyerOrg&needFields%5B%5D=accountingOrg&needFields%5B%5D=updateDate&needFields%5B%5D=receiveDate&needFields%5B%5D=operatingTime"
            callback = "jQuery331_" + str(time_tag)
            r = requests.post("http://www.bse.cn/projectNewsController/infoResult.do?callback=" + callback, data=param, headers=headers)
            json_data = json.loads(r.text.replace(callback,"").replace("([","[").replace("])","]"))

            for d in json_data:
                page_max = int(d["listInfo"]["totalPages"])
                
                for stock in d["listInfo"]["content"]:
                    stock_code = stock["stockCode"]
                    stock_name = stock["stockName"]
                    self.add_stock(stock_code, stock_name)
                if page_max <= page:
                    return False
                return True
        except Exception as e:
            print(str(e))
            return False
        
        
    def get_data(self):
        page = 0
        time.sleep(3)
        while self.query_page_data(page):
            print(page)
            page = page + 1
            time.sleep(3)

if __name__ == '__main__':
    d = BeijingStock()
    d.get_data()
